package com.situ.web.servlet;

import java.io.IOException;
import java.nio.channels.IllegalSelectorException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.BanJi;
import com.situ.web.entity.Banji_Course;
import com.situ.web.entity.Course;
import com.situ.web.service.ISelectBCService;
import com.situ.web.service.impl.SelectBCServiceImpl;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import net.sf.json.JSONArray;

public class SelectBanJiCourseServlet extends HttpServlet {
	private ISelectBCService selectBCService = new SelectBCServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectBanJi":
			selectBanJi(req,resp);
			break;
		case "selectCourse":
			selectCourse(req,resp);
			break;
		case "add":
			add(req,resp);
			break;
		default:
			break;
		}
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String banjiId = req.getParameter("banjiId");
		String courseId = req.getParameter("courseId");
		boolean result = false;
		result = selectBCService.add(Integer.parseInt(banjiId),Integer.parseInt(courseId));
		System.out.println(result);
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/banJiCourse");
		}
	}

	private void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Course> list = selectBCService.findAllCourses();
		for (Course course : list) {
			System.out.println(course);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectBanJi(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<BanJi> list = selectBCService.findAllBanJi();
		for (BanJi banJi : list) {
			System.out.println(banJi);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

}
